package myblog.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import myblog.dao.ReviewDao;
import myblog.entity.ReviewEntity;
import myblog.vo.ReviewVo;

public class ReviewDaoImpl extends JdbcDaoSupport implements ReviewDao {

	@Override
	public List<ReviewEntity> list() {
		String sql = "select * from review";

		JdbcTemplate jt = getJdbcTemplate();

		List<ReviewEntity> reviewList = jt.query(sql, new ReviewRowMapper());

		return reviewList;
	}
	
	class ReviewRowMapper implements RowMapper {
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

			ReviewEntity entity = new ReviewEntity();

			entity.setReviewid(rs.getString("reviewid"));
			entity.setLogid(rs.getString("logid"));
			entity.setReviewcontent(rs.getString("reviewcontent"));
			entity.setReviewname(rs.getString("reviewname"));
			entity.setReviewdate(rs.getString("reviewdate"));
			return entity;
		}
	}

	@Override
	public void addReview(ReviewVo reviewVo) {
		JdbcTemplate jt = getJdbcTemplate();

		String sql = "insert into review(logid,reviewcontent,reviewname) values(?,?,?)";

		// 构建参数数组
		Object[] args = { reviewVo.getLogid(),reviewVo.getReviewcontent(),reviewVo.getReviewname() };

		jt.update(sql, args);
		
	}

}
